package com.dgshanger.sanhxiaofeisc.ui.person

import android.annotation.SuppressLint
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.entity.MyTeamEntity
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.xx.baseuilibrary.adapter.BaseRecycleAdapter
import com.xx.baseuilibrary.adapter.BaseViewHolder
import com.xx.baseuilibrary.mvp.contract.BaseMvpRecycleViewContract
import com.xx.baseuilibrary.mvp.lcec.BaseMvpRecycleViewFragment
import com.xx.baseutilslibrary.network.entity.BaseResponseEntity
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import io.reactivex.Observable
import kotlinx.android.synthetic.main.item_my_team_inviter.view.*


/**
 * MyTeamInviterListFragment
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/4/16 13:39.
 */
class MyTeamInviterListFragment : BaseMvpRecycleViewFragment<MyTeamEntity.ListRcdBean>() {
    override val listLimit: Int
        get() = 10
    override val layoutManager: RecyclerView.LayoutManager
        get() = LinearLayoutManager(mContext)

    override fun createAdapter(): BaseRecycleAdapter<MyTeamEntity.ListRcdBean, out BaseViewHolder<MyTeamEntity.ListRcdBean>> = Adapter().apply { setLoadMoreEnable(true) }

    override fun createModel(): BaseMvpRecycleViewContract.Model<MyTeamEntity.ListRcdBean> = Model()

    class Model : BaseMvpRecycleViewContract.Model<MyTeamEntity.ListRcdBean> {
        @SuppressLint("NewApi")
        override fun loadData(page: Int, httpObserver: XxBaseHttpObserver<List<MyTeamEntity.ListRcdBean>>?) =
                AppApi.Api()
                        .my_team(page, "shop")
                        .flatMap({ myTeamEntityBaseResponseEntity ->
                            Observable.just(BaseResponseEntity(1, myTeamEntityBaseResponseEntity.data!!.list_rcd))
                        })
                        .compose(RxHelper.io_main())
                        .subscribe(httpObserver)

    }

    class Adapter : BaseRecycleAdapter<MyTeamEntity.ListRcdBean, ViewHolder>() {
        override fun createViewHolder(view: View?, viewType: Int): ViewHolder = ViewHolder(view!!)

        override fun getItemLayout(viewType: Int): Int = R.layout.item_my_team_inviter

    }

    class ViewHolder(view: View) : BaseViewHolder<MyTeamEntity.ListRcdBean>(view) {
        @SuppressLint("SetTextI18n")
        override fun bindView(position: Int, entity: MyTeamEntity.ListRcdBean?) {
            with(entity!!) {
                ImageLoader.loadAvatar(itemView.context, itemView.iv_menu_img, menu_img)
                itemView.tv_shop_address.text = shop_address
                itemView.tv_sales_total.text = "累计业绩: $sales_total"
            }
        }

    }
}